package com.commerce.recording.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;

import com.commerce.recording.R;

import java.util.Timer;
import java.util.TimerTask;

public class WelcomeActivity extends AppCompatActivity implements SensorEventListener {

    private static final Paint sPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
    private Bitmap bitmap;
    private ImageView img;
    private SensorManager sensorManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        img =findViewById(R.id.welcome_img);
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
             unregisterSensor();
             startActivity(new Intent(WelcomeActivity.this,MainActivity.class));
             finish();
            }
        },3*1000);
        sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        //通过传感器管理器获取重力加速度传感器
        sensorManager.registerListener(this,sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),SensorManager.SENSOR_DELAY_NORMAL);
    }


    @Override
    public void onSensorChanged(SensorEvent event) {
        if(event.sensor.getType() != Sensor.TYPE_ACCELEROMETER){
            return;
        }
        //更改图片颜色
        float[] values = event.values;
        float x = values[0]+10;
        float y = values[1]+10;
        float z = values[2]+10;
        if (bitmap == null) {
            bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.welcome);
        }
        Bitmap bm = createBitmapSafely(bitmap.getWidth(), bitmap.getHeight());
        Canvas canvas = new Canvas();
        canvas.setBitmap(bm);
        Paint p = sPaint;
        PorterDuffColorFilter porterDuffColorFilter = null;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            porterDuffColorFilter = new PorterDuffColorFilter(Color.rgb(x/20*255,y/20*255,z/20*255), PorterDuff.Mode.SRC_ATOP);
        }
        p.setColorFilter(porterDuffColorFilter);
        canvas.drawBitmap(bitmap, 0, 0, p);
        p.setColorFilter(null);
        canvas.setBitmap(null);
        img.setImageBitmap(bm);
    }

    //创建一个图片 宽 高 色彩模式（4*8 =32位）
    private static Bitmap createBitmapSafely(int width, int height) {
        try {
            return Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        } catch (OutOfMemoryError error) {
            error.printStackTrace();
        }
        return null;
    }
    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }

   //注销传感器
    public void unregisterSensor(){
        if(sensorManager!=null){
            sensorManager.unregisterListener(this);
        }
    }
}